import {createSlice} from "@reduxjs/toolkit"

const counterStore = createSlice({
   name:"counter",
   //初始化sate
   initialState:{
    count:0
   },
   //修改状态的方法 同步方法 支持直接修改
   reducers:{
    increment(state){
      
        state.count++
    },
    decrement(state){
        if(state.count){
            state.count--
        }else{
            console.log(state.count)
        }
       
    },
    addToNums(state,action){
        state.count += action.payload
    }
   }
})

//结构出来actionCreater函数
const {increment,decrement,addToNums} = counterStore.actions
//获取reducer
const reducer = counterStore.reducer

export {increment,decrement,addToNums} 
export default reducer